#include <SDL.h>
#include "controller.h"

Controller::Controller():
  env( SDL_putenv(const_cast<char*>("SDL_VIDEO_CENTERED=center")) ),
  io(IOManager::getInstance()),
  screen( io->getScreen() ),
  model(),
  gui()
  { }

Controller::~Controller() {
  atexit(SDL_Quit);
}

void Controller::drawBackground() const {
  model.getWorld().draw();
}

string Controller::menu(){
  bool keyCatch = false; // To get only 1 key per keydown
  SDL_Event event;
  bool done = false;
  bool returnKey = false;

  while ( not done ) {

    drawBackground();
    gui.draw();
    SDL_Flip(screen);
	
    SDL_PollEvent(&event);
    if (event.type ==  SDL_QUIT) { done = true;return "quit";}
    if(event.type == SDL_KEYDOWN) {
      switch ( event.key.keysym.sym ) {
        //case SDLK_ESCAPE : done = true; break;
        //case SDLK_q      : done = true; break;
        case SDLK_RETURN : case SDLK_SPACE:{
          if ( !keyCatch ) {
            gui.lightOn();
            returnKey = true;
          }
          break;
        }
        case SDLK_DOWN   : {
          if ( !keyCatch ) {
            gui.increment();
          }
          break;
        }
        case SDLK_UP   : {
          if ( !keyCatch ) {
            gui.decrement();
          }
          break;
        }
        default          : break;
      }
      keyCatch = true;
    }
    if(event.type == SDL_KEYUP) { 
      keyCatch = false; 
      if ( returnKey ) {
        SDL_Delay(250);
        //done = true;
        return gui.getIconClicked();
      }
      gui.lightOff();
    }
  }
}

void Controller::play() {
  SDL_Event event;
  bool keyCatch = true;
  bool done = false;
  model.start();
  while ( ! done ) {
	model.draw();
	model.update();
    SDL_PollEvent(&event);
    Uint8 *keystate = SDL_GetKeyState(NULL);

    switch (event.type) {
      case SDL_QUIT: {
        done = true;
        break;
      }
      case SDL_KEYUP: {
		//if((keystate[SDLK_UP] )||(keystate[SDLK_DOWN] )||(keystate[SDLK_LEFT] )||(keystate[SDLK_RIGHT] )){
			keyCatch = true;
			model.stop();

		//}
      }
      case SDL_KEYDOWN: {
        if (keystate[SDLK_q] /*|| keystate[SDLK_ESCAPE]*/) {
          done = true;
        }
        if (keystate[SDLK_ESCAPE]) {
			if(!model.isPaused()){
				model.toggleHelp();
				
			}	
			
			menu();
        }
        if ( (keystate[SDLK_F1] || keystate[SDLK_p] )&& keyCatch) {
		  model.toggleHelp();
		  keyCatch = false;
        }
        
        if ((keystate[SDLK_m]) && keyCatch) {
			  model.toggleBgm();
			  keyCatch = false;
			}
			
        if(!model.isPaused())
        {
			

			//if ( keystate[SDLK_UP] ) {
			  //model.move(1);
			//}
			//if ( keystate[SDLK_DOWN] ) {
			  //model.move(2);
			//}
			if ( keystate[SDLK_LEFT] ) {
			  model.move(3);
			}
			if ( keystate[SDLK_RIGHT] ) {
			  model.move(4);
			}
			if ( keystate[SDLK_e] && keyCatch ) {
			  model.explode();
			  keyCatch = false;
			}
			
			if ( keystate[SDLK_r] && keyCatch ) {
			  model.resetBricks();
			  keyCatch = false;
			}
			
			if((keystate[SDLK_1] || keystate[SDLK_2] || keystate[SDLK_3])
				&& keyCatch)
			{
				int sound = event.key.keysym.sym - SDLK_0;
				model.playSound(sound);
				keyCatch = false;
			}
			if(keystate[SDLK_SPACE] && keyCatch)
			{
				
				model.emitBall();
				keyCatch = false;
			}
		}
        
        break;
      }
    }
  }
}
